/**
 * 当前登陆用户管理
 * @author LinZongxue
 * @create_date 2011-12-25
 */
define(function(){
	var CurLoginUserPanel = Ext.extend(Ext.grid.GridPanel, {
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    loadMask: true,
		initComponent: function(){
			var grid = this;
			this.tbar = [
			    {id:'curCount', xtype:'label',text:'当前登陆数：0', style:'margin-right:15px;'},'-',
			    {text: '刷新', handler: this.refresh, iconCls: 'icon-refresh',scope: this}
			];
			this.store = new Ext.data.JsonStore({
			    remoteSort: true,
			    idProperty: 'id',
			    fields: ['userId', 'loginName', 'userName', 'departName', 'loginTime', 'ipAddress', 'browser', 'sessionId'],
			    url: 'sys/user/getCurrentLoginInfoList.do',
			    listeners: {
			    	load: function(store, records){
			    		grid.getTopToolbar().get('curCount').setText('当前登陆总数：' + records.length);
			    	}
			    }
			});
			this.columns = [
				new Ext.grid.RowNumberer(),
				{header: '账号名', width: 90, dataIndex: 'loginName'},
				{header: '用户姓名', width: 80, dataIndex: 'userName'},
				{header: '所属机构', width: 120, dataIndex: 'departName'},
				{header: '登陆时间', width: 120, dataIndex: 'loginTime', renderer: lu.formatTimeDefault},
				{header: '登陆IP', width: 100, dataIndex: 'ipAddress'},
				{header: '浏览器', width: 100, dataIndex: 'browser'},
				{header: '会话ID', width: 100, dataIndex: 'sessionId'},
				{
					xtype: 'actioncolumn',width: 30,
					dataIndex: 'sessionId',
					items: [{
	                    icon: cu.getIconImg('delete.gif'),
	                    tooltip: '踢除用户',
	                    handler: function(grid, rowIndex, colIndex) {
	                        var record = grid.getStore().getAt(rowIndex);
	                        grid.killSessions(record);
	                    }
	                }]
				}
			];
			CurLoginUserPanel.superclass.initComponent.call(this);
			this.refresh();
		},
		refresh: function(){
			this.getStore().load();
		},
		queryRecords: function(){
			
		},
		killSessions: function(record){
			if (record.json.sessionId == au.getLoginInfo().sessionId){
				cu.alert('不能踢除自己');
				return;
			}
			var msg = '确定要踢除用户' + record.json.loginName + '吗？' + 
				'<br/><nobr>会话ID：' + record.json.sessionId + '</nobr>';
			cu.confirm(msg, function(){
				cu.post({
					url: 'sys/user/killSession.do', 
					params: {sessionId: record.json.sessionId},
					success: function(){
						this.refresh();
						cu.hint('踢除用户成功');
					}, 
					scope: this
				});
			}, this);
		}
	});
	return CurLoginUserPanel;
});